﻿using CpcParser;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace CpcSchemaParser
{
    public class EntryPoint
    {
        private List<string> parsed;
        private Queue<XElement> next;
        private List<CpcClass> sql;

        public EntryPoint()
        {
            this.parsed = new List<string>();
            this.next = new Queue<XElement>();
            this.sql = new List<CpcClass>();
        }

        static void Main(string[] args)
        {
            string path = @"C:\Users\Filip\Documents\visual studio 2015\Projects\CpcParser\CpcParser\XML\cpc-scheme-A01B.xml";
            var engine = new EntryPoint();
            // var result = 
            engine.ReadXML(path);

        }

        private void ReadXML(string filePath)
        {
            XDocument xdoc = XDocument.Load(filePath);

            //var res = xdoc.Descendants("classification-item")
            //    .Select(x =>
            //    new
            //    {
            //        Name = (x.FirstNode as XElement).Value,
            //        Description = string.Join("; ",(x as XElement).Descendants("class-title").SelectMany(y => y.Descendants("text")).Select(y => (y as XElement).Value)),
            //        Level = x.Attribute("level").Value,
            //    }).ToList();

            //var first = res.First();

            ParseItem(xdoc.Descendants("classification-item").First());
          

            using (var writer = new StreamWriter("CPC.sql", false, Encoding.Unicode))
            {
              
                var strs = this.sql.ToList().Select(x => string.Format("insert into cpcs values ('{0}','{1}','{2}');", x.Name.Replace("'", "''"), x.Level, x.Description.Replace("'", "''")));
                strs.ToList().ForEach(x => writer.WriteLine(x));
            }
             
        }

        public void ParseItem(XElement item)
        {
            var symbol = item.Descendants("classification-symbol").FirstOrDefault();
            var title = item.Descendants("class-title").FirstOrDefault().Value;
            var level = item.Attribute("level").Value;

            var elems = item.Descendants("classification-item").ToList();

            this.sql.Add(new CpcClass {
                 Name = symbol.Value,
                 Description = title,
                  Level = int.Parse(level)
            });

            this.parsed.Add(symbol.Value);

            elems.ForEach(x => this.next.Enqueue(x));

            if (this.next.Count == 0) return;

            var next = this.next.Dequeue();
            var nextSymbol = next.Descendants("classification-symbol").FirstOrDefault().Value;
            while(this.parsed.Contains(nextSymbol))
            {
            if (this.next.Count == 0) return;

                next = this.next.Dequeue();
                nextSymbol = next.Descendants("classification-symbol").FirstOrDefault().Value;
            }

            this.ParseItem(next);

           
        }



     
        
    }
}
